<?php
/**
 * Created by PhpStorm.
 * User: hetu
 * Date: 2018/5/7
 * Time: 10:12
 */

use yii\helpers\Url ;
use backend\service\RbacService ;
use backend\assets\AppAsset ;
use yii\helpers\Html ;
use yii\widgets\LinkPager;

$this->params['breadcrumbs'][] = "模块管理";
$this->params['display_name'] = "角色列表" ;
$this->title = '角色列表-' . Yii::$app->params['webname'];

$i = 1 ;
$role_name = Yii::$app->request->get('role_name');
?>
<div class="row">
    <div class="col-md-12">
        <div class="nav-tabs-custom">
            <div class="tab-content">
                <div class="tab-pane active">
                    <form method="get" class="form-inline form" id="form" action="<?= Url::toRoute(["system/role-list"]) ?>">
                        <div class="box-body">
                            <div class="form-group">
                                    <label class="control-label">角色名称：</label>
                                    <input type="role_name" class="form-control" id="role_name" name="role_name" value="<?php if(isset($role_name)){echo $role_name;}else{echo '';}?>" maxlength="8" />
                            </div>
                            <div class="form-group">
                                <button class="btn btn-primary" type="submit">查询</button>
                                <button id="add_new" class="btn btn-primary btn-hide button-add-role" title="添加角色" type="button">+添加角色</button>
                            </div>
                        </div>
                    </form>
                    <div class="box-body table-responsive no-padding">
                        <span>共<?= $pages->totalCount ?>条记录</span>
                        <table class="table table-bordered table-striped table-condensed">
                            <tbody>
                                <tr>
                                    <th>序号</th>
                                    <th>角色名称</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                                <?php foreach ($roles as $key=>$value):?>
                                <tr>
                                    <td><?= $i?></td>
                                    <td class="center"><?= $value->name?></td>
                                    <td class="center"><?= date("Y-m-d",$value->created_at)?></td>
                                    <td class="center">
                                        <button id="edit-role" class="btn btn-primary btn-xs upd btn-hide button-upd-role" title="编辑角色" type="button" data-id="<?= $key?>">编辑</button>
                                        <button class="btn btn-primary btn-xs del-role-btn btn-hide button-del-role" data-id="<?= $key?>" type="button">删除</button>
                                    </td>
                                </tr>
                                <?php $i=$i+1?>
                                <?php endforeach;?>
                                <tr>
                                    <td colspan="17" class="text-center">
                                        <?=
                                        LinkPager::widget([
                                            'firstPageLabel' => '首页',
                                            'lastPageLabel' => '末页',
                                            'prevPageLabel' => '上一页',
                                            'nextPageLabel' => '下一页',
                                            'pagination' =>$pages,
                                            'maxButtonCount' => 8,
                                        ])
                                        ?>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div><!--/span-->
<script>
    var search_query = window.location.search.replace('?', '');
    var role_name;
    $(function(){
        $("#myModal").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
            $(this).find(".modal-content").children().remove();
        })
    });
    $('#add_new').click(function () {
        var options = {remote: '<?= Url::toRoute('add-role') ?>'};
        $('#myModal').modal(options)
    });
    $('.upd').click(function () {
        var role_name = $(this).data('id');
        var options = {remote: '<?= Url::toRoute('upd-role') ?>' + '?role_name=' + role_name};
        $('#myModal').modal(options)
    });

    $('.del-role-btn').click(function () {
        role_name = $(this).data('id');
        var tmpHtml = '<div class="modal-header">\n' +
            '                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n' +
            '                <h4 class="modal-title">会员之家后台管理</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-body">\n' +
            '                <h4>您确定要删除此角色吗?</h4>\n' +
            '            </div>\n' +
            '            <div class="modal-footer">\n' +
            '                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>\n' +
            '                <button type="button" class="btn btn-default modal-btn-sure button-del-role-confirm">确认</button>\n' +
            '            </div>';
        $("#myModal").find(".modal-content").html(tmpHtml);
        $('#myModal').modal('show');
    });
    $(document).on('click', '.button-del-role-confirm', function () {
        var name =  role_name;
        sendAjax('<?= Url::toRoute('del-role') ?>' + "?" + search_query, {role_name:name}, $(this));
    })


</script>